import babelpolyfill from 'babel-polyfill'
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//import './assets/theme/theme-green/index.css'
import VueRouter from 'vue-router'
import store from './vuex/store'
import Vuex from 'vuex'
//配置axios的全局基本路径 t
// axios.defaults.baseURL='http://localhost:8080'
//import NProgress from 'nprogress'
//import 'nprogress/nprogress.css'
import routes from './routes'
/*import Mock from './mock'
Mock.bootstrap();*/
import 'font-awesome/css/font-awesome.min.css'

Vue.use(ElementUI)
Vue.use(VueRouter)
Vue.use(Vuex)

//NProgress.configure({ showSpinner: false });

/*引入axios*/
import axios from 'axios'
axios.defaults.baseURL="http://localhost:8080"
/*===============axios拦截器========================*/
/*====前置拦截====*/
axios.interceptors.request.use(config=>{
    /*携带token*/
    let utoken = localStorage.getItem("token");
    if(utoken){
      config.headers['token']=utoken;
    }
    return config;
},error => {
  Promise.reject(error);
})

/*====后置拦截====*/
axios.interceptors.response.use(result=>{
    let data = result.data;
    if(!data.success && data.message=='noLogin'){
        localStorage.getItem("token");
        localStorage.getItem("loginInfo");
        router.push({path: '/login'})
    }
    return result;
},error => {
    Promise.reject(error);
})

/*++++++++++++axios拦截器++++++++++++*/
// axios.defaults.baseURL='/api' //使用反向代理解决跨域问题
//全局属性配置，在任意组件内可以使用this.$http获取axios对象
Vue.prototype.$http = axios

const router = new VueRouter({
    routes
})
/*路由前置拦截(静态资源拦截)*/
router.beforeEach((to, from, next) => {
  if (to.path == '/login' || to.path == '/shopRegister') {
    /*携带了的直接放行*/
    localStorage.removeItem('token');
    localStorage.removeItem('loginInfo');
    next()
  }else {
    /*未携带的，看是否是登录状态，是就放行*/
    let user = localStorage.getItem("loginInfo");
    if(!user){
      // 没有数据代表没有登录，跳转登陆页面
        next({path:'/login'})
    }else {
      next();
    }
  }
})

//router.afterEach(transition => {
//NProgress.done();
//});

new Vue({
  //el: '#app',
  //template: '<App/>',
  router,
  store,
  //components: { App }
  render: h => h(App)
}).$mount('#app')

